// pages/tasks/list/list.js
const app = getApp();
const globalData = getApp().globalData;
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const taskService = require("../../../apiservice/task/taskservice")
var util = require('../../../utils/util.js');


Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		order: 'desc',
		height: '',
		total: 0,
		displaytotal: 0,
		fixTop: '', //区域离顶部的高度
		scrollTop: 0, //滑动条离顶部的距离
		isnextPage: false,
		//是否可以滚动
		canlower: true, 
		showPopup: false,
		//切换图片
		switchImageValue: 0,
		switchImageUrl: "/images/pages/date_down.png",
		floorstatus: false,
	},

	async GetPage(pageNum, pageSize) {
            
		try {
			var result = await taskService.getTaskPage(pageNum, pageSize, this.data.order)
			if (result.data && result.data && result.data.datas) {
				//debugger;
				var displaytotal = this.data.displaytotal + result.data.datas.length;
				this.setData({
					['list[' + (pageNum - 1) + ']']: result.data.datas,
					pagenum: pageNum,
					displaytotal: displaytotal,
					isnextPage: result.data.existsnextdata,
					total: result.data.total,
					canlower: true
				})
				console.log(result.data);
			} else {
				this.setData({
					pagenum: 0,
					displaytotal: 0,
					isnextPage: 0,
					total: 0,
					isnextPage: true,
					canlower: true
				})
			}

		} catch (e) {
			//TODO handle the exception
			if (e.statusCode == 401) {
				wx.redirectTo({
					url: "../../login/login"
				})
			}
			this.setData({
				canlower: true
			})
		} finally {
			wx.hideLoading();
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
	
		let that=this;
		let res = wx.getSystemInfoSync();
        let query = wx.createSelectorQuery();
		query.select('#banner').boundingClientRect(function(rect){
			//底部的导航栏计算公式:
			// 底部tabBar的高度
			let tabBarHeight = res.screenHeight - res.windowHeight-6;
	  
			//debugger
			that.setData({
				height: res.windowHeight -tabBarHeight -rect.width/2.36,
				displaytotal: 0
			});
		}).exec();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

		
		
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading',
			mask: true
		});
		
		if (typeof this.getTabBar === 'function' &&
			this.getTabBar()) {
			this.getTabBar().setData({
				selected: "index"
			})
		}

		
		this.setData({
			['list']: null,
			pagenum: 0,
			displaytotal: 0,
			isnextPage: 0,
			total: 0,
			isnextPage: false,
			floorstatus: false,
		})

		let userInfo = globalData.userInfo;
		let mobilerole = userInfo.mobilerole_systemuser;

    // if(userInfo.site=="爱尔眼科医院集团"){ //集团
    //   let roleIds = [1, 2, 3];
    //   this.showTabBarItem(mobilerole, roleIds, "showScreens");

    //   roleIds = [5, 4, 7];
    //   this.showTabBarItem(mobilerole, roleIds, "showPromotion");
    // }
    // else{  //医院  医院配了筛查，则学术推广也显示
    //   let roleIds = [1, 2, 3];  //筛查
    //   if (util.haveRole(mobilerole, roleIds)) {
    //     if (typeof this.getTabBar === 'function' &&
    //       this.getTabBar()) {
    //       this.getTabBar().setData({
    //         ["showScreens"]: true,
    //         ["showPromotion"]:true
    //       })
    //     }
    //   }
    //   else{ //医院没有配置筛查
    //     this.getTabBar().setData({
    //       ["showScreens"]: false
    //     })
    //     roleIds = [5, 4, 7];
    //     this.showTabBarItem(mobilerole, roleIds, "showPromotion");
    //   }
    // }
    // let roleIds = [12, 13];
    // this.showTabBarItem(mobilerole, roleIds, "showFollow");
    let roleIds = [1, 2, 3];
    this.showTabBarItem(mobilerole, roleIds, "showScreens");
    roleIds = [5, 4, 7, 13]; //13: 村医
    this.showTabBarItem(mobilerole, roleIds, "showPromotion");
    roleIds = [12];
    this.showTabBarItem(mobilerole, roleIds, "showFollow");
		this.GetPage(1, 20);
	},

	showTabBarItem: function(mobilerole, roleIds, attrName) {
		let that = this;
		if (util.haveRole(mobilerole, roleIds)) {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: true
				})
			}
		} else {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: false
				})
			}
		}
	},

	lower: async function() {
		//debugger;
		if (this.data.isnextPage) {
			if(this.data.canlower)
			{
				wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
					title: '加载中',
					icon: 'loading',
					mask: true
				});
				var pageNum = this.data.pagenum;
				this.setData({
					canlower: false
				});
				await this.GetPage(++pageNum, 20);
				
				if (this.data.pagenum > 1) {
					this.setData({
						floorstatus: true
					});
				
				} else {
					this.setData({
						floorstatus: false
					});
				}
			}
	
			//wx.hideLoading();
		} else {
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '我也是有底线的',
				icon: 'success',
				duration: 350
			});
			return false;
		}
	},




	onClose: function(e) {
		this.setData({
			showPopup: false,
		})
	},

	onOpen: function(e) {
		this.setData({
			showPopup: true,
		})
	},

	onSort: function(e) {
		let currOrder = e.currentTarget.dataset.order;
		let preOrder = this.data.order
		let url = "";
		let orderValue = "";

		if (currOrder === "desc") {
			url = "/images/pages/date_up.png",
				orderValue = 'asc';
		} else if (currOrder === "asc") {
			url = "/images/pages/date_down.png",
				orderValue = 'desc';
		}
		this.setData({
			switchImageUrl: url,
			order: orderValue
		});
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
			title: '加载中',
			icon: 'loading',
			mask: true
		});
		this.onShow();

	},

	goTop: function(e) { // 一键回到顶部
		if (wx.pageScrollTo) {
			wx.pageScrollTo({
				scrollTop: 0
			})
			this.setData({
				scrollTop: 0
			});
		} else {
			wx.showModal({
				title: '提示',
				content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
			})
		}
	},


	onPageScroll(e) {

	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},



	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},

	cancelClick: function(e) {
		console.info(e.detail.taskId);
		var that = this;
		wx.showModal({
			title: '提示',
			content: '您确定要取消任务吗？',
			success: function(sm) {
				if (sm.confirm) {
					console.log(e)
					that.cancelTask(e.detail.taskId);
				} else if (sm.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},

	cancelTask: function(taskId) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
			title: '加载中',
			icon: 'loading',
			mask: true
		});
		var that = this;
		var entityName = "task";
		var state = 2;
		var status = 6;
		taskService.UpdateState(taskId, entityName, state, status).then(function(result) {
			console.log(result);
			that.onShow();
		}).catch(function(error) {

		})
	},


})
